package com.nannybear;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.nannybear.Communication.Connection;
import com.nannybear.Controller.ViewPuppeteer;
import com.nannybear.Model.LightThreshold;
import com.nannybear.Model.SoundThreshold;
import com.nannybear.View.BabyStateScreen;
import com.nannybear.View.BrightnessScreen;
import com.nannybear.View.HelpScreen;
import com.nannybear.View.HomeScreen;
import com.nannybear.View.HumidityScreen;
import com.nannybear.View.Screen;
import com.nannybear.View.SettingsScreen;
import com.nannybear.View.TempScreen;

/**
 * @brief Main activity
 */
public class MainActivity extends Activity {
    /********** DECLARATIONS **********/
    // Variables
    private static Context context;
   // private String ip = "192.168.42.1";       // RED BRICK
   // private int port = 12345;                   // RED BRICK
    private String ip = "10.2.36.82";           //SERVER PC
    private int port = 4444;                    // SERVER PC

    // View classes
    private Screen currentScreen;
    private HomeScreen homeScreen;
    private TempScreen tempScreen;
    private BrightnessScreen brightnessScreen;
    private HumidityScreen humidityScreen;
    private BabyStateScreen babyStateScreen;
    private SettingsScreen settingsScreen;
    private HelpScreen helpScreen;

    // Controller classes
    private ViewPuppeteer viewPuppeteer;

    // Communication classes
    private Connection connection;

    // Model classes
    private SoundThreshold soundThreshold;
    private LightThreshold lightThreshold;


    /**
     * @brief Initialization of the connection.
     */
    public void initializationConnection(){
        this.connection = new Connection(this.ip, this.port);
        connection.start();
        try{
            connection.join();
        }catch (InterruptedException ex){
            Log.e("MainActivity", "Interruption when it try to join to thread of Connection");
        }
    }

    /**
     * @brief Load the activity.
     */
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        try{
            MainActivity.context = getApplicationContext();
        	this.setContentView(R.layout.activity_main);

            // Connection creation
            initializationConnection();

        	//View creation
            this.homeScreen = new HomeScreen();
            this.tempScreen = new TempScreen();
            this.brightnessScreen = new BrightnessScreen();
            this.humidityScreen = new HumidityScreen();
            this.babyStateScreen = new BabyStateScreen();
            this.settingsScreen = new SettingsScreen();
            this.helpScreen = new HelpScreen();
            this.currentScreen = this.homeScreen;

            //Model creation
            this.lightThreshold = new LightThreshold();
            this.soundThreshold = new SoundThreshold();

            //Controller creation
            this.viewPuppeteer = new ViewPuppeteer(this);

            // Add observer
            this.lightThreshold.addObserver(this.connection.getProxyRobot());
            this.soundThreshold.addObserver(this.connection.getProxyRobot());

        }catch (Exception ex){
            Log.e("MainActivity", "Problem on create : "+ex.getMessage());
        }
    }

    /**
     * @brief Destroy the activity.
     */
    protected void onDestroy(){
        super.onDestroy();
        this.connection.closeConnection();
    }


    /*********** ACCESSORS ***********/
    public void setCurrentScreen(Screen currentScr) {
        this.currentScreen = currentScr;
    }

    public Screen getCurrentScreen() {
        return currentScreen;
    }

    public void setHomeScreen(HomeScreen homeScr){
        this.homeScreen = homeScr;
    }

    public HomeScreen getHomeScreen() {
        return homeScreen;
    }

    public void setTempScreen(TempScreen tempScr) {
        this.tempScreen = tempScr;
    }

    public TempScreen getTempScreen() {
        return tempScreen;
    }

    public void setBrightnessScreen(BrightnessScreen brightnessScr){
        this.brightnessScreen = brightnessScr;
    }

    public BrightnessScreen getBrightnessScreen() {
        return brightnessScreen;
    }

    public void setHumidityScreen(HumidityScreen humidityScr) {
        this.humidityScreen = humidityScr;
    }

    public HumidityScreen getHumidityScreen() {
        return humidityScreen;
    }

    public void setBabyStateScreen(BabyStateScreen babyStateScr) {
        this.babyStateScreen = babyStateScr;
    }

    public BabyStateScreen getBabyStateScreen() {
        return babyStateScreen;
    }

    public void setSettingsScreen(SettingsScreen settingsScr) {
        this.settingsScreen = settingsScr;
    }

    public SettingsScreen getSettingsScreen() {
        return settingsScreen;
    }

    public void setHelpScreen(HelpScreen helpScr) {
        this.helpScreen = helpScr;
    }

    public HelpScreen getHelpScreen() {
        return helpScreen;
    }

    public static Context getAppContext(){
        return MainActivity.context;
    }

    public LightThreshold getLightThreshold() {
        return lightThreshold;
    }

    public SoundThreshold getSoundThreshold() {
        return soundThreshold;
    }
}